home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Snippets / Sound / SndPlayDoubleBuffer / _source / DoubleBufferFromFile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-15  |  30.2 KB  |  1,100 lines  |  [TEXT/CWIE]

  1. /*
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    Routines demonstrating how to play a sound from disk using SndPlayDoubleBuffer.
  5. **
  6. **    by Mark Cookson, Apple Developer Technical Support
  7. **
  8. **    File:    DoubleBufferFromFile.c
  9. **
  10. **    Copyright ©1996 Apple Computer, Inc.
  11. **    All rights reserved.
  12. **
  13. **    You may incorporate this sample code into your applications without
  14. **    restriction, though the sample code has been provided "AS IS" and the
  15. **    responsibility for its operation is 100% yours.  However, what you are
  16. **    not permitted to do is to redistribute the source as "Apple Sample
  17. **    Code" after having made changes. If you're going to re-distribute the
  18. **    source, we require that you make it clear in the source that the code
  19. **    was descended from Apple Sample Code, but that you've made changes.
  20. */
  21.  
  22. #include "DoubleBufferFromFile.h"
  23.  
  24. /*    Purpose:        This creates a new SoundInfo structure and initializes
  25.                     it by calling the private function ASoundInit.
  26.     Side Effects:    None.
  27. */
  28. /*-----------------------------------------------------------------------*/
  29. SoundInfoPtr    ASoundNew                (OSErr *theErr)
  30. /*-----------------------------------------------------------------------*/
  31. {
  32.     SoundInfoPtr    theSoundInfo    = nil;
  33.  
  34.     *theErr = noErr;
  35.     theSoundInfo = (SoundInfoPtr)NewPtrClear (sizeof (SoundInfo));
  36.  
  37.     if (MemError () != noErr || theSoundInfo == nil) {
  38.         *theErr = kInitErr;
  39.     }
  40.     else {
  41.         *theErr = ASoundInit (theSoundInfo);
  42.     }
  43.  
  44.     if (*theErr != noErr) {
  45.         DebugPrint ("\pError in ASoundNew");
  46.     }
  47.  
  48.     return theSoundInfo;
  49. }
  50.  
  51. /*
  52.     Purpose:        Display a StandardFile dialog to select a file.
  53.                     Opens the file selected by the user.
  54.     Side Effects:    None.
  55. */
  56. /*-----------------------------------------------------------------------*/
  57. OSErr            ASoundGetFileToPlay        (SoundInfoPtr theSoundInfo)
  58. /*-----------------------------------------------------------------------*/
  59. {
  60.     FileFilterUPP        myFilterUPP    = nil;
  61.     OSErr                theErr        = noErr;
  62.     short                i            = 0;
  63.     Boolean                good        = false;
  64.  
  65.     if (IsValid (theSoundInfo)) {
  66.         myFilterUPP = NewFileFilterProc (ASoundFileFilter);
  67.         if ((theSoundInfo->globals.ggestaltStandardFileAttr & gestaltStandardFile58) == false) {
  68.             StandardFileReply    theSFReply;
  69.             short                showTypes;
  70.  
  71.             if ((theSoundInfo->globals.ggestaltTranslationAttr & (1 << gestaltTranslationMgrExists)) == true) {
  72.                 showTypes = kUseOpenResourceTypes;
  73.             }
  74.             else {
  75.                 showTypes = kNoFirstFiltering;
  76.             }
  77.             StandardGetFile (myFilterUPP, showTypes, nil, &theSFReply);
  78.             theSoundInfo->vRefNum = theSFReply.sfFile.vRefNum;
  79.             if (theSFReply.sfGood == true) {
  80.                 switch (theSFReply.sfType) {
  81.                     case kSNDResource:
  82.                     case kResource:
  83.                         theErr = FSpOpenRF (&theSFReply.sfFile, fsCurPerm, &(theSoundInfo->refNum));
  84.                         break;
  85.                     case kCompressedAIFFFile:
  86.                     case kUncompressedAIFFFile:
  87.                     case kWAVEFile:
  88.                     case kWAVFile:
  89.                     case kAUFile:
  90.                         theErr = FSpOpenDF (&theSFReply.sfFile, fsCurPerm, &(theSoundInfo->refNum));
  91.                         break;
  92.                     default:
  93.                         DebugPrint ("\pASoundGetFileToPlay should have never executed this line");
  94.                 }
  95.                 if (theErr != noErr) {
  96.                     DebugPrint ("\pCouldn't open file");
  97.                 }
  98.                 else {
  99.                     theSoundInfo->fileType = theSFReply.sfType;
  100.                     for (i = 0; i <= theSFReply.sfFile.name[0]+1; i++)
  101.                         theSoundInfo->theName[i] = theSFReply.sfFile.name[i];
  102.                 }
  103.             }
  104.             else {
  105.                 theErr = userCanceledErr;
  106.             }
  107.         }
  108.         else {
  109.             SFReply            theSFReply;
  110.             Point            where        = {kInit, kInit};
  111.             Rect            screenRect    = GetMainScreenRect();
  112.             long            dirID        = kInit,
  113.                             procID        = kInit;
  114.  
  115.             where.h = screenRect.right / kHorizAdjust;    /* This will put the SFGetFile dialog in the same place */
  116.             where.v = screenRect.bottom / kVertAdjust;    /* as the StandardGetFile dialog (or really close). */
  117.             if ((GetMBarHeight() + kOne) > where.h) {
  118.                 where.h = GetMBarHeight() + kOne;
  119.             }
  120.             SFGetFile (where, nil, myFilterUPP, kNoFirstFiltering, nil, nil, &theSFReply);
  121.             good = theSFReply.good;
  122.             if (theSFReply.good == true) {
  123.                 theErr = GetWDInfo (theSFReply.vRefNum, &(theSoundInfo->vRefNum), &dirID, &procID);
  124.                 if (theErr == noErr) {
  125.                     switch (theSFReply.fType) {
  126.                         case kSNDResource:
  127.                         case kResource:
  128.                             theErr = HOpenRF (theSoundInfo->vRefNum, dirID, theSFReply.fName, fsCurPerm, &(theSoundInfo->refNum));
  129.                             break;
  130.                         case kCompressedAIFFFile:
  131.                         case kUncompressedAIFFFile:
  132.                         case kWAVEFile:
  133.                         case kWAVFile:
  134.                         case kAUFile:
  135.                             theErr = HOpenDF (theSoundInfo->vRefNum, dirID, theSFReply.fName, fsCurPerm, &(theSoundInfo->refNum));
  136.                             break;
  137.                         default:
  138.                             DebugPrint ("\pASoundGetFileToPlay should have never executed this line");
  139.                     }
  140.                     if (theErr != noErr) {
  141.                         DebugPrint ("\pCouldn't open file");
  142.                     }
  143.                     else {
  144.                         theSoundInfo->fileType = theSFReply.fType;
  145.                         for (i = 0; i <= theSFReply.fName[0]+1; i++)
  146.                             theSoundInfo->theName[i] = theSFReply.fName[i];
  147.                     }
  148.                 }
  149.                 else {
  150.                     theErr = kFileErr;
  151.                     DebugPrint ("\pCouldn't translate working directory");
  152.                 }
  153.             }
  154.             else {
  155.                 theErr = userCanceledErr;
  156.             }
  157.         }
  158.         DisposeRoutineDescriptor (myFilterUPP);
  159.     }
  160.     else {
  161.         theErr = kNilPtrErr;
  162.     }
  163.  
  164.     if (theErr != noErr && theErr != userCanceledErr) {
  165.         DebugPrint ("\pError in ASoundGetFileToPlay");
  166.     }
  167.  
  168.     return theErr;
  169. }
  170.  
  171. /*
  172.     Purpose:        Checks a file to see if its header can be parsed
  173.                     and the file can be played.
  174.  
  175.                     This will return an error if the sound will not play,
  176.                     returning noErr means that sound will play.
  177.  
  178.                     Parsing a header can take some time, this routine
  179.                     is a canidate for speed improvements.
  180.     Side Effects:    None.
  181. */
  182. /*-----------------------------------------------------------------------*/
  183. OSErr            ASoundCanThisPlay        (CInfoPBPtr theFileInfo)
  184. /*-----------------------------------------------------------------------*/
  185. {
  186.     SoundInfoPtr    theSoundInfo    = nil;
  187.     long            dataStart        = kInit,
  188.                     sndLength        = kInit;
  189.     OSErr            theErr            = noErr,
  190.                     closeErr        = noErr;
  191.  
  192.     if (theFileInfo  != nil) {
  193.         theSoundInfo = ASoundNew (&theErr);
  194.         if (theErr == noErr) {
  195.             theSoundInfo->vRefNum = theFileInfo->hFileInfo.ioVRefNum;
  196.             theSoundInfo->fileType = theFileInfo->hFileInfo.ioFlFndrInfo.fdType;
  197.             theErr = HOpenDF (theFileInfo->hFileInfo.ioVRefNum, theFileInfo->hFileInfo.ioFlParID, theFileInfo->hFileInfo.ioNamePtr, fsRdPerm, &theSoundInfo->refNum);
  198.             if (theErr == noErr) {
  199.                 switch (theSoundInfo->fileType) {
  200.                     case kCompressedAIFFFile:
  201.                     case kUncompressedAIFFFile:
  202.                         theErr = ASoundGetAIFFHeader (theSoundInfo, &dataStart, &sndLength);
  203.                         break;
  204.                     case kWAVEFile:
  205.                     case kWAVFile:
  206.                         theErr = ASoundGetWAVEHeader (theSoundInfo, &dataStart, &sndLength);
  207.                         break;
  208.                     case kAUFile:
  209.                         theErr = ASoundGetULAWHeader (theSoundInfo, &dataStart, &sndLength);
  210.                         break;
  211.                     case kSNDResource:
  212.                     case kResource:
  213.                         theErr = noErr;
  214.                         break;
  215.                     default:
  216.                         /* The file type is not one that we can even parse. */
  217.                         theErr = kUnknownFormat;
  218.                         break;
  219.                 }
  220.             }
  221.         }
  222.     }
  223.     else {
  224.         theErr = kNilPtrErr;
  225.     }
  226.  
  227.     closeErr = FSClose (theSoundInfo->refNum);
  228.     DisposeRoutineDescriptor (ASoundGetSoundCallBack (theSoundInfo));
  229.     DisposePtr ((Ptr)theSoundInfo);
  230.  
  231.     if (closeErr != noErr) {
  232.         theErr = closeErr;
  233.     }
  234.  
  235.     if (theErr != noErr && theErr != kUnknownFormat) {
  236.         DebugPrint ("\pError in ASoundCanThisPlay");
  237.     }
  238.  
  239.     return theErr;
  240. }
  241.  
  242. /*
  243.     Purpose:        This function is called to get ready to play a sound.
  244.                     Use this if you want to make sure that there is enough
  245.                     memory to play the sound.
  246.     Side Effects:    This will call routines that will allocate memory needed
  247.                     to all of the various structures needed by the Sound Manager
  248.                     and memory to be used as the sounds' buffers.
  249. */
  250. /*-----------------------------------------------------------------------*/
  251. OSErr            ASoundReadyForPlaying    (SoundInfoPtr theSoundInfo,
  252.                                         unsigned long bufferSize)
  253. /*-----------------------------------------------------------------------*/
  254. {
  255.     OSErr                theErr        = noErr;
  256.  
  257.     if (IsValid (theSoundInfo)) {
  258.         if (theSoundInfo->globals.gSupportsSPDB == false) {
  259.             DebugPrint ("\pThis machine doesn't support SoundPlayDoubleBuffer, can't play!");
  260.             theErr = kNoSPDBErr;
  261.         }
  262.         else {
  263.             /* do we want to start at the begining of the file? */
  264.             if ((ASoundGetBytesCopied (theSoundInfo) <= kMaxAIFFHeaderSize) && theSoundInfo->paused == false) {
  265.                 theSoundInfo->soundDone = false;
  266.                 theErr = SndNewChannel (&(theSoundInfo->chan), sampledSynth, nil, ASoundGetSoundCallBack (theSoundInfo));
  267.                 if (theErr != noErr) {
  268.                     DebugPrint ("\pSndNewChannel error!");
  269.                 }
  270.                 else {
  271.                     theErr = SetUpSoundHeader (theSoundInfo, bufferSize);
  272.                     if (theErr == noErr) {
  273.                         theErr = ASoundPrimeBuffers (theSoundInfo);
  274.                         ASoundSetCurBuffer (theSoundInfo, kStart);
  275.                     }
  276.                 }
  277.             }
  278.             else {
  279.                 theErr = ASoundPrimeBuffers (theSoundInfo);
  280.             }
  281.         }
  282.     }
  283.     else {
  284.         theErr = kNilPtrErr;
  285.     }
  286.  
  287.     if (theErr != noErr) {
  288.         DebugPrint ("\pError in ASoundReadyForPlaying!");
  289.         (void)ASoundDonePlaying (theSoundInfo, kCloseFile + kFreeMem);
  290.     }
  291.  
  292.     return theErr;
  293. }
  294.  
  295. /*
  296.     Purpose:        Call this after you have called ASoundReadyForPlaying to
  297.                     start playing the sound you prepaired.
  298.     Side Effects:    Starts the sound playing.
  299. */
  300. /*-----------------------------------------------------------------------*/
  301. OSErr            ASoundPlay                (SoundInfoPtr theSoundInfo)
  302. /*-----------------------------------------------------------------------*/
  303. {
  304.     OSErr                theErr        = noErr;
  305.  
  306.     if (IsValid (theSoundInfo)) {
  307.         if (theSoundInfo->playing == false || theSoundInfo->paused == true) {
  308.             if (theSoundInfo->globals.gSupportsSPDB == false) {
  309.                 theErr = kNoSPDBErr;
  310.                 DebugPrint ("\pThis machine doesn't support SoundPlayDoubleBuffer, can't play!");
  311.             }
  312.             else {
  313.                 if (theSoundInfo->paused == false) {
  314.                     theErr = SndPlayDoubleBuffer (theSoundInfo->chan, (SndDoubleBufferHeaderPtr)&(theSoundInfo->doubleHeader));
  315.                     if (theErr == noErr) {
  316.                         theErr = InstallCallBack (theSoundInfo);
  317.                     }
  318.                     if (theErr == noErr) {
  319.                         theSoundInfo->playing = true;
  320.                     }
  321.                 }
  322.             }
  323.         }
  324.     }
  325.     else {
  326.         theErr = kNilPtrErr;
  327.     }
  328.  
  329.     if (theErr != noErr) {
  330.         DebugPrint ("\pError in ASoundPlay!");
  331.         (void)ASoundDonePlaying (theSoundInfo, kCloseFile + kFreeMem);
  332.     }
  333.  
  334.     return theErr;
  335. }
  336.  
  337. /*
  338.     Purpose:        Wrapper function called to start playing a sound.
  339.                     Use this if you are pretty sure the sound will play, or
  340.                     just don't care specifically what goes wrong.
  341.     Side Effects:    This will call routines that will allocate memory needed
  342.                     for all of the various structures needed by the Sound Manager
  343.                     and memory to be used as the sounds' buffers.
  344. */
  345. /*-----------------------------------------------------------------------*/
  346. OSErr            ASoundStartPlaying        (SoundInfoPtr theSoundInfo,
  347.                                         unsigned long bufferSize)
  348. /*-----------------------------------------------------------------------*/
  349. {
  350.     OSErr                theErr        = noErr;
  351.  
  352.     theErr = ASoundReadyForPlaying (theSoundInfo, bufferSize);
  353.     if (theErr == noErr) {
  354.         theErr = ASoundPlay (theSoundInfo);
  355.     }
  356.  
  357.     if (theErr != noErr) {
  358.         DebugPrint ("\pError in ASoundStartPlaying!");
  359.         (void)ASoundDonePlaying (theSoundInfo, kCloseFile + kFreeMem);
  360.     }
  361.  
  362.     return theErr;
  363. }
  364.  
  365. /*
  366.     Purpose:        Stops the currently playing sound.
  367.     Side Effects:    Stopping the currently playing sound will cause the
  368.                     sound completion routine to run.
  369. */
  370. /*-----------------------------------------------------------------------*/
  371. OSErr            ASoundStop                (SoundInfoPtr theSoundInfo)
  372. /*-----------------------------------------------------------------------*/
  373. {
  374.     SndCommand        theCmd        = {quietCmd, kInit, kInit};
  375.     OSErr            theErr        = noErr;
  376.  
  377.     if (StrictIsValid (theSoundInfo)) {
  378.         /* So that ASoundDoubleBackProc knows that it doesn't have to
  379.            actually read any data */
  380.         theSoundInfo->stopping = true;
  381.         theErr = SndDoImmediate (theSoundInfo->chan, &theCmd);
  382.         if (theErr == noErr) {
  383.             theSoundInfo->playing = false;
  384.             theSoundInfo->paused = false;
  385.         }
  386.     }
  387.     else {
  388.         theErr = kNilPtrErr;
  389.     }
  390.  
  391.     if (theErr != noErr) {
  392.         DebugPrint ("\pError in ASoundStop");
  393.     }
  394.  
  395.     return theErr;
  396. }
  397.  
  398. /*
  399.     Purpose:        Wrapper so the user doesn't have to keep track of if
  400.                     the sound is playing or not.
  401.     Side Effects:    If resuming a sound and the user had also called
  402.                     ASoundPauseForAdjust this will reinstall the sound
  403.                     completion callback.
  404. */
  405. /*-----------------------------------------------------------------------*/
  406. OSErr            ASoundPause                (SoundInfoPtr theSoundInfo)
  407. /*-----------------------------------------------------------------------*/
  408. {
  409.     OSErr            theErr        = noErr;
  410.  
  411.     if (StrictIsValid (theSoundInfo)) {
  412.         if (theSoundInfo->paused == false) {
  413.             theErr = PauseSound (theSoundInfo);
  414.         }
  415.         else {
  416.             theErr = ResumeSound (theSoundInfo);
  417.         }
  418.     }
  419.     else {
  420.         theErr = kNilPtrErr;
  421.     }
  422.  
  423.     if (theErr != noErr) {
  424.         DebugPrint ("\pError in ASoundPause");
  425.     }
  426.  
  427.     return theErr;
  428. }
  429.  
  430. /*
  431.     Purpose:        If the sound is paused, resume playing.  If the sound is
  432.                     playing, pause playing.
  433.                     This differs from ASoundPause because it actually stops
  434.                     the sound instead of pausing it.  When the sound is
  435.                     paused for adjusting you can reset where the sound will
  436.                     next start playing from without having to play the
  437.                     remainder of the current buffer.  This routine is used
  438.                     for the QuickTime style playing.
  439.     Side Effects:    Removes the callback from the sound channel because
  440.                     otherwise while adjusting the sound the Sound Manager
  441.                     would call our clean up routine.
  442.                     When resuming a sound ASoundStartPlaying will install
  443.                     our callback routine if necessary (if the sound wasn't
  444.                     already paused).
  445. */
  446. /*-----------------------------------------------------------------------*/
  447. OSErr            ASoundPauseForAdjust    (SoundInfoPtr theSoundInfo)
  448. /*-----------------------------------------------------------------------*/
  449. {
  450.     SndCommand        theCmd            = {kInit, kInit, kInit};
  451.     OSErr            theErr            = noErr;
  452.     static Fixed    oldRate            = kInit;
  453.  
  454.     if (StrictIsValid (theSoundInfo)) {
  455.         if (theSoundInfo->adjusting == false) {
  456.             theCmd.cmd = flushCmd;            /* so the sound completion callback doesn't get called */
  457.             theErr = SndDoImmediate (theSoundInfo->chan, &theCmd);
  458.             if (theErr == noErr) {
  459.                 theSoundInfo->adjusting = true;
  460.                 theCmd.cmd = quietCmd;
  461.                 theErr = SndDoImmediate (theSoundInfo->chan, &theCmd);
  462.                 if (theErr == noErr) {
  463.                     theSoundInfo->hasBeenAdjusted = true;
  464.                     theSoundInfo->playing = false;
  465.                 }
  466.             }
  467.         }
  468.         else {
  469.             theSoundInfo->adjusting = false;
  470.             ASoundStartPlaying (theSoundInfo, nil);
  471.         }
  472.     }
  473.     else {
  474.         theErr = kNilPtrErr;
  475.     }
  476.  
  477.     if (theErr != noErr) {
  478.         DebugPrint ("\pError in ASoundPauseForAdjust");
  479.     }
  480.  
  481.     return theErr;
  482. }
  483.  
  484. /*
  485.     Purpose:        Sound is done playing, dispose of the memory we no
  486.                     longer need.
  487.     Side Effects:    None.
  488. */
  489. /*-----------------------------------------------------------------------*/
  490. OSErr            ASoundDonePlaying        (SoundInfoPtr theSoundInfo,
  491.                                         unsigned long options)
  492. /*-----------------------------------------------------------------------*/
  493. {
  494.     myParamBlockRec    *myPB            = nil;
  495.     OSErr            theErr            = noErr;
  496.     short             i                = kInit,
  497.                     savedVRefNum    = kInit,
  498.                     savedRefNum        = kInit;
  499.  
  500.     if (IsValid (theSoundInfo)) {
  501.         theSoundInfo->soundDone = false;            /* so we don't get called multiple times */
  502.         theSoundInfo->playing = false;
  503.  
  504.         savedVRefNum = theSoundInfo->vRefNum;
  505.         savedRefNum = theSoundInfo->refNum;
  506.         if ((options == kCloseFile) || (options > kCloseFile + kFreeMem)) {
  507.             DebugPrint ("\pInvalid selector passed to ASoundDonePlaying");
  508.             theErr = kBadValue;
  509.         }
  510.         else {
  511.             theSoundInfo->stopping = false;    /* Sound is done stopping, hopefully... */
  512.             if (options > kNoOptions) {
  513.                 myPB = (myParamBlockRec*)theSoundInfo->doubleHeader.dbhBufferPtr[kDBBufOne]->dbUserInfo[kPBPtr];
  514.                 if (myPB != nil) {
  515.                     if (options == kCloseFile + kFreeMem) {
  516.                         theErr = FSClose (myPB->pb.ioParam.ioRefNum);
  517.                     }
  518.                     if (theErr == noErr) {
  519.                         for (i = kInit; i <= kOne; i++) {
  520.                             DisposeRoutineDescriptor (((myParamBlockRec*)theSoundInfo->doubleHeader.dbhBufferPtr[i]->dbUserInfo[kPBPtr])->pb.ioParam.ioCompletion);
  521.                             DisposePtr ((Ptr)theSoundInfo->doubleHeader.dbhBufferPtr[i]->dbUserInfo[kPBPtr]);
  522.                             /* Have to unhold memory that was held */
  523.                             UnholdMemory ((Ptr)theSoundInfo->doubleHeader.dbhBufferPtr[i], sizeof(SndDoubleBuffer) + ASoundGetBufferSize (theSoundInfo));
  524.                             DisposePtr ((Ptr)theSoundInfo->doubleHeader.dbhBufferPtr[i]);
  525.                         }
  526.                     }
  527.                     else {
  528.                         DebugPrint ("\pFSClose error!");
  529.                     }
  530.                     theErr = SndDisposeChannel (theSoundInfo->chan, true);
  531.                     DisposeRoutineDescriptor (ASoundGetSoundCallBack (theSoundInfo));
  532.                     DisposeRoutineDescriptor (theSoundInfo->doubleHeader.dbhDoubleBack);
  533.                     if (theErr != noErr) {
  534.                         DebugPrint ("\pSndDisposeChannel error!");
  535.                     }
  536.                 }
  537.             }
  538.  
  539.             if (options == kNoOptions) {
  540.                 ASoundSetBytesCopied (theSoundInfo, theSoundInfo->dataStart);
  541.                 ASoundSetCurBuffer (theSoundInfo, kStart);
  542.                 theSoundInfo->doubleHeader.dbhBufferPtr[kDBBufOne]->dbFlags = nil;
  543.                 theSoundInfo->doubleHeader.dbhBufferPtr[kDBBufTwo]->dbFlags = nil;
  544.                 theErr = ASoundPrimeBuffers (theSoundInfo);
  545.             }
  546.             if (options == kFreeMem) {
  547.                 ASoundInit (theSoundInfo);
  548.                 theSoundInfo->vRefNum = savedVRefNum;
  549.                 theSoundInfo->refNum = savedRefNum;
  550.             }
  551.             else {
  552.                 if (options == kCloseFile + kFreeMem) {
  553.                     ASoundInit (theSoundInfo);
  554.                 }
  555.             }
  556.         }
  557.     }
  558.     else {
  559.         theErr = kNilPtrErr;
  560.     }
  561.  
  562.     if (theErr != noErr) {
  563.         DebugPrint ("\pError in ASoundDonePlaying");
  564.     }
  565.  
  566.     return theErr;
  567. }
  568.  
  569. /*
  570.     Purpose:        Returns the channel for the sound in case you want to
  571.                     send it specific commands.
  572.     Side Effects:    None.
  573. */
  574. /*-----------------------------------------------------------------------*/
  575. SndChannelPtr    ASoundGetChan            (SoundInfoPtr theSoundInfo)
  576. /*-----------------------------------------------------------------------*/
  577. {
  578.     SndChannelPtr    returnValue;
  579.  
  580.     if (StrictIsValid (theSoundInfo)) {
  581.         returnValue = theSoundInfo->chan;
  582.     } else {
  583.         returnValue = nil;
  584.     }
  585.  
  586.     return returnValue;
  587. }
  588.  
  589. /*
  590.     Purpose:        Returns the name of the file containing the currently
  591.                     playing sound.
  592.     Side Effects:    None.
  593. */
  594. /*-----------------------------------------------------------------------*/
  595. OSErr            ASoundGetSoundName        (SoundInfoPtr theSoundInfo,
  596.                                         Str63 theName)
  597. /*-----------------------------------------------------------------------*/
  598. {
  599.     OSErr            theErr        = noErr;
  600.     short            i            = 0;
  601.  
  602.     if (StrictIsValid (theSoundInfo) && theName != nil) {
  603.         /* This should probably be a BlockMoveData */
  604.         for (i = 0; i <= theSoundInfo->theName[0]+1; i++)
  605.             theName[i] = theSoundInfo->theName[i];
  606.     }
  607.  
  608.     return theErr;
  609. }
  610.  
  611. /*
  612.     Purpose:        Gets the number of the current buffer
  613.                     (in the range 1 to ASoundGetNumBuffers()) of the
  614.                     currently playing sound.
  615.     Side Effects:    None.
  616. */
  617. /*-----------------------------------------------------------------------*/
  618. long            ASoundGetCurBuffer        (SoundInfoPtr theSoundInfo)
  619. /*-----------------------------------------------------------------------*/
  620. {
  621.     long        returnValue        = kInit;
  622.     OSErr        theErr            = noErr;
  623.  
  624.     if (StrictIsValid (theSoundInfo)) {
  625.         returnValue = theSoundInfo->currentBuffer;
  626.     }
  627.     else {
  628.         theErr = kNilPtrErr;
  629.         returnValue = nil;
  630.     }
  631.  
  632.     if (theErr != noErr) {
  633.         DebugPrint ("\pError in ASoundGetCurBuffer");
  634.     }
  635.  
  636.     return returnValue;
  637. }
  638.  
  639. /*
  640.     Purpose:        Sets which buffer should be the next buffer to play
  641.                     from (in the range 1 to ASoundGetNumBuffers())
  642.                     for the currently playing sound.
  643.     Side Effects:    None.
  644. */
  645. /*-----------------------------------------------------------------------*/
  646. OSErr            ASoundSetCurBuffer        (SoundInfoPtr theSoundInfo,
  647.                                         long newValue)
  648. /*-----------------------------------------------------------------------*/
  649. {
  650.     OSErr        theErr            = noErr;
  651.  
  652.     if (IsValid (theSoundInfo)) {
  653.         if ((newValue >= kStart) && (newValue <= ASoundGetNumBuffers (theSoundInfo))) {
  654.             theSoundInfo->currentBuffer = newValue;
  655.         }
  656.         else {
  657.             theErr = kBadRange;
  658.         }
  659.     }
  660.     else {
  661.         theErr = kNilPtrErr;
  662.     }
  663.  
  664.     return theErr;
  665. }
  666.  
  667. /*
  668.     Purpose:        Gets the number of buffers that the currently playing
  669.                     sound will need to play in its entirety.
  670.     Side Effects:    None.
  671. */
  672. /*-----------------------------------------------------------------------*/
  673. long            ASoundGetNumBuffers        (SoundInfoPtr theSoundInfo)
  674. /*-----------------------------------------------------------------------*/
  675. {
  676.     long        returnValue        = kInit;
  677.     OSErr        theErr            = noErr;
  678.  
  679.     if (IsValid (theSoundInfo)) {
  680.         returnValue = theSoundInfo->numBuffers;
  681.     }
  682.     else {
  683.         theErr = kNilPtrErr;
  684.         returnValue = nil;
  685.     }
  686.  
  687.     if (theErr != noErr) {
  688.         DebugPrint ("\pError in ASoundGetNumBuffers");
  689.     }
  690.  
  691.     return returnValue;
  692. }
  693.  
  694. /*
  695.     Purpose:        Gets the length (in bytes) of the currently playing
  696.                     sound.  This number does not include any header bytes.
  697.     Side Effects:    None.
  698. */
  699. /*-----------------------------------------------------------------------*/
  700. long            ASoundGetNumTotalBytes    (SoundInfoPtr theSoundInfo)
  701. /*-----------------------------------------------------------------------*/
  702. {
  703.     long        returnValue        = kInit;
  704.     OSErr        theErr            = noErr;
  705.  
  706.     if (IsValid (theSoundInfo)) {
  707.         returnValue = theSoundInfo->bytesTotal;
  708.     }
  709.     else {
  710.         theErr = kNilPtrErr;
  711.         returnValue = nil;
  712.     }
  713.  
  714.     if (theErr != noErr) {
  715.         DebugPrint ("\pError in ASoundGetNumTotalBytes");
  716.     }
  717.  
  718.     return returnValue;
  719. }
  720.  
  721. /*
  722.     Purpose:        Gets the number of bytes that will be played by the end
  723.                     of the current buffer of the currently playing sound.
  724.     Side Effects:    None.
  725. */
  726. /*-----------------------------------------------------------------------*/
  727. long            ASoundGetBytesCopied    (SoundInfoPtr theSoundInfo)
  728. /*-----------------------------------------------------------------------*/
  729. {
  730.     long        returnValue        = kInit;
  731.     OSErr        theErr            = noErr;
  732.  
  733.     if (IsValid (theSoundInfo)) {
  734.         returnValue = theSoundInfo->bytesCopied;
  735.     }
  736.     else {
  737.         theErr = kNilPtrErr;
  738.         returnValue = nil;
  739.     }
  740.  
  741.     if (theErr != noErr) {
  742.         DebugPrint ("\pError in ASoundGetBytesCopied");
  743.     }
  744.  
  745.     return returnValue;
  746. }
  747.  
  748. /*
  749.     Purpose:        Sets the location in the file where the next buffer
  750.                     should be filled from for the currently playing sound.
  751.     Side Effects:    None.
  752. */
  753. /*-----------------------------------------------------------------------*/
  754. OSErr            ASoundSetBytesCopied    (SoundInfoPtr theSoundInfo,
  755.                                         long newValue)
  756. /*-----------------------------------------------------------------------*/
  757. {
  758.     OSErr        theErr            = noErr;
  759.  
  760.     if (IsValid (theSoundInfo)) {
  761.         if (newValue >= theSoundInfo->dataStart || newValue == kInit) {
  762.             theSoundInfo->bytesCopied = newValue;
  763.         }
  764.         else {
  765.             theErr = kBadValue;
  766.         }
  767.     }
  768.     else {
  769.         theErr = kNilPtrErr;
  770.     }
  771.  
  772.     if (theErr != noErr) {
  773.         DebugPrint ("\pError in ASoundSetBytesCopied");
  774.     }
  775.  
  776.     return theErr;
  777. }
  778.  
  779. /*
  780.     Purpose:        Gets the size of a buffer of the currently playing
  781.                     sound.  Multiply by two to know how much memory is
  782.                     reserved for buffering the currently playing sound.
  783.     Side Effects:    None.
  784. */
  785. /*-----------------------------------------------------------------------*/
  786. long            ASoundGetBufferSize        (SoundInfoPtr theSoundInfo)
  787. /*-----------------------------------------------------------------------*/
  788. {
  789.     long        returnValue        = kInit;
  790.     OSErr        theErr            = noErr;
  791.  
  792.     if (IsValid (theSoundInfo)) {
  793.         returnValue = theSoundInfo->doubleBufferSize;
  794.     }
  795.     else {
  796.         theErr = kNilPtrErr;
  797.         returnValue = nil;
  798.     }
  799.  
  800.     if (theErr != noErr) {
  801.         DebugPrint ("\pError in ASoundGetBufferSize");
  802.     }
  803.  
  804.     return returnValue;
  805. }
  806.  
  807. /*
  808.     Purpose:        Gets the UPP for the function that should be called when
  809.                     the currently playing sound finishes.
  810.     Side Effects:    None.
  811. */
  812. /*-----------------------------------------------------------------------*/
  813. SndCallBackUPP    ASoundGetSoundCallBack    (SoundInfoPtr theSoundInfo)
  814. /*-----------------------------------------------------------------------*/
  815. {
  816.     SndCallBackUPP        returnValue        = nil;
  817.     OSErr                theErr            = noErr;
  818.  
  819.     if (IsValid (theSoundInfo)) {
  820.         returnValue = theSoundInfo->theSoundCallBackUPP;
  821.     }
  822.     else {
  823.         theErr = kNilPtrErr;
  824.         returnValue = nil;
  825.     }
  826.  
  827.     if (theErr != noErr) {
  828.         DebugPrint ("\pError in ASoundGetSoundCallBack");
  829.     }
  830.  
  831.     return returnValue;
  832. }
  833.  
  834. /*
  835.     Purpose:        Sets the function that should be called when the the
  836.                     currently playing sound finishes.
  837.     Side Effects:    None.
  838. */
  839. /*-----------------------------------------------------------------------*/
  840. OSErr            ASoundSetSoundCallBack    (SoundInfoPtr theSoundInfo,
  841.                                         void* newValue)
  842. /*-----------------------------------------------------------------------*/
  843. {
  844.     OSErr        theErr        = noErr;
  845.  
  846.     if (IsValid (theSoundInfo)) {
  847.         if (theSoundInfo->theSoundCallBackUPP != nil) {
  848.             DisposeRoutineDescriptor (theSoundInfo->theSoundCallBackUPP);
  849.         }
  850.         if (newValue != nil) {
  851.             theSoundInfo->theSoundCallBackUPP = NewSndCallBackProc(newValue);
  852.         }
  853.         else {
  854.             theSoundInfo->theSoundCallBackUPP = NewSndCallBackProc(ASoundDoneCallBack);
  855.             DebugPrint ("\pDid you really want the default sound callback?");
  856.         }
  857.     }
  858.     else {
  859.         theErr = kNilPtrErr;
  860.     }
  861.  
  862.     if (theErr != noErr) {
  863.         DebugPrint ("\pError in ASoundSetSoundCallBack");
  864.     }
  865.  
  866.     return theErr;
  867. }
  868.  
  869. /*
  870.     Purpose:        Says whether to play the currently playing sound backwards
  871.                     (it actually reverses the sound in the buffer).
  872.     Side Effects:    Takes effect when the next sound buffer gets filled.
  873. */
  874. /*-----------------------------------------------------------------------*/
  875. OSErr            ASoundPlayBackwards        (SoundInfoPtr theSoundInfo,
  876.                                         Boolean newValue)
  877. /*-----------------------------------------------------------------------*/
  878. {
  879.     OSErr        theErr        = noErr;
  880.  
  881.     if (StrictIsValid (theSoundInfo)) {
  882.         theSoundInfo->backwards = newValue;
  883.     }
  884.     else {
  885.         theErr = kNilPtrErr;
  886.     }
  887.  
  888.     if (theErr != noErr) {
  889.         DebugPrint ("\pError in ASoundPlayBackwards");
  890.     }
  891.  
  892.     return theErr;
  893. }
  894.  
  895. /*
  896.     Purpose:        Returns true if the currently playing sound's buffer
  897.                     is set to be reversed.
  898.     Side Effects:    None.
  899. */
  900. /*-----------------------------------------------------------------------*/
  901. Boolean            ASoundIsBackwards        (SoundInfoPtr theSoundInfo)
  902. /*-----------------------------------------------------------------------*/
  903. {
  904.     OSErr        theErr            = noErr;
  905.     Boolean        returnValue        = false;
  906.  
  907.     if (StrictIsValid (theSoundInfo)) {
  908.         returnValue = theSoundInfo->backwards;
  909.     }
  910.     else {
  911.         theErr = kNilPtrErr;
  912.     }
  913.  
  914.     if (theErr != noErr) {
  915.         DebugPrint ("\pError in ASoundIsBackwards");
  916.     }
  917.  
  918.     return returnValue;
  919. }
  920.  
  921. /*
  922.     Purpose:        Returns true if the sound has finished playing.
  923.     Side Effects:    None.
  924. */
  925. /*-----------------------------------------------------------------------*/
  926. Boolean            ASoundIsDone            (SoundInfoPtr theSoundInfo)
  927. /*-----------------------------------------------------------------------*/
  928. {
  929.     OSErr        theErr            = noErr;
  930.     Boolean        returnValue        = false;
  931.  
  932.     if (StrictIsValid (theSoundInfo)) {
  933.         returnValue = theSoundInfo->soundDone;
  934.     }
  935.     else {
  936.         theErr = kNilPtrErr;
  937.     }
  938.  
  939.     if (theErr != noErr) {
  940.         DebugPrint ("\pError in ASoundIsDone");
  941.     }
  942.  
  943.     return returnValue;
  944. }
  945.  
  946. /*
  947.     Purpose:        Changes the volume of the currently playing sound.
  948.                     The values you pass in are added to the current values.
  949.                     Negitive values will decrease the volume, positive values
  950.                     will increase the volume.
  951.     Side Effects:    None.
  952. */
  953. /*-----------------------------------------------------------------------*/
  954. OSErr            ASoundChangeVolume        (SoundInfoPtr theSoundInfo,
  955.                                         unsigned short leftVol,
  956.                                         unsigned short rightVol)
  957. /*-----------------------------------------------------------------------*/
  958. {
  959.     SndCommand        theCmd        = {volumeCmd, kInit, kInit};
  960.     OSErr            theErr        = noErr;
  961.     unsigned short    tempLeft    = kInit,
  962.                     tempRight    = kInit;
  963.  
  964.     if (StrictIsValid (theSoundInfo)) {
  965.         theErr = ASoundGetVolume (theSoundInfo, &tempLeft, &tempRight);
  966.         if (theErr == noErr) {
  967.             if ((tempLeft + leftVol) > kMaxVolume) leftVol = kMaxVolume - tempLeft;
  968.             if ((tempRight + rightVol) > kMaxVolume) rightVol = kMaxVolume - tempRight;
  969.             if ((tempLeft + leftVol) < kMinVolume) leftVol = -tempLeft;
  970.             if ((tempRight + rightVol) < kMinVolume) rightVol = -tempRight;
  971.             theErr = ASoundSetVolume (theSoundInfo, tempLeft+leftVol, tempRight+rightVol);
  972.         }
  973.     }
  974.     else {
  975.         theErr = kNilPtrErr;
  976.     }
  977.  
  978.     if (theErr != noErr) {
  979.         DebugPrint ("\pError in ASoundChangeVolume");
  980.     }
  981.  
  982.     return theErr;
  983. }
  984.  
  985. /*
  986.     Purpose:        Gets the volume of the currently playing sound.
  987.     Side Effects:    None.
  988. */
  989. /*-----------------------------------------------------------------------*/
  990. OSErr            ASoundGetVolume            (SoundInfoPtr theSoundInfo,
  991.                                         unsigned short *leftVol,
  992.                                         unsigned short *rightVol)
  993. /*-----------------------------------------------------------------------*/
  994. {
  995.     SndCommand        theCmd        = {getVolumeCmd, kInit, kInit};
  996.     unsigned long    theVol        = kInit;
  997.     OSErr            theErr        = noErr;
  998.  
  999.     if (StrictIsValid (theSoundInfo)) {
  1000.         theCmd.param2 = (long)&theVol;
  1001.         theErr = SndDoImmediate(theSoundInfo->chan, &theCmd);
  1002.  
  1003.         if (theErr == noErr) {
  1004.             *leftVol = theVol & kLeftMask;
  1005.             *rightVol = theVol >> kSixteen;
  1006.         }
  1007.     }
  1008.     else {
  1009.         theErr = kNilPtrErr;
  1010.     }
  1011.  
  1012.     if (theErr != noErr) {
  1013.         DebugPrint ("\pError in ASoundGetVolume");
  1014.     }
  1015.  
  1016.     return theErr;
  1017. }
  1018.  
  1019. /*
  1020.     Purpose:        Sets the volume of the currently playing sound.
  1021.     Side Effects:    None.
  1022. */
  1023. /*-----------------------------------------------------------------------*/
  1024. OSErr            ASoundSetVolume            (SoundInfoPtr theSoundInfo,
  1025.                                         unsigned short leftVol,
  1026.                                         unsigned short rightVol)
  1027. /*-----------------------------------------------------------------------*/
  1028. {
  1029.     SndCommand        theCmd        = {volumeCmd, kInit, kInit};
  1030.     OSErr            theErr        = noErr;
  1031.  
  1032.     if (StrictIsValid (theSoundInfo)) {
  1033.         theCmd.param2 = (rightVol << kSixteen) | leftVol;
  1034.         theErr = SndDoImmediate(theSoundInfo->chan, &theCmd);
  1035.     }
  1036.     else {
  1037.         theErr = kNilPtrErr;
  1038.     }
  1039.  
  1040.     if (theErr != noErr) {
  1041.         DebugPrint ("\pError in ASoundSetVolume");
  1042.     }
  1043.  
  1044.     return theErr;
  1045. }
  1046.  
  1047. /*
  1048.     Purpose:        Gets the rate multiplier of the currently playing sound.
  1049.     Side Effects:    None.
  1050. */
  1051. /*-----------------------------------------------------------------------*/
  1052. OSErr            ASoundGetRateMul        (SoundInfoPtr theSoundInfo,
  1053.                                         UnsignedFixed *theRateMul)
  1054. /*-----------------------------------------------------------------------*/
  1055. {
  1056.     SndCommand        theCmd        = {getRateMultiplierCmd, kInit, kInit};
  1057.     OSErr            theErr        = noErr;
  1058.  
  1059.     if (StrictIsValid (theSoundInfo)) {
  1060.         theCmd.param2 = (long)theRateMul;
  1061.         theErr = SndDoImmediate(theSoundInfo->chan, &theCmd);
  1062.     }
  1063.     else {
  1064.         theErr = kNilPtrErr;
  1065.     }
  1066.  
  1067.     if (theErr != noErr) {
  1068.         DebugPrint ("\pError in ASoundGetRateMul");
  1069.     }
  1070.  
  1071.     return theErr;
  1072. }
  1073.  
  1074. /*
  1075.     Purpose:        Gets the rate multiplier of the currently playing sound.
  1076.     Side Effects:    None.
  1077. */
  1078. /*-----------------------------------------------------------------------*/
  1079. OSErr            ASoundSetRateMul        (SoundInfoPtr theSoundInfo,
  1080.                                         UnsignedFixed theRateMul)
  1081. /*-----------------------------------------------------------------------*/
  1082. {
  1083.     SndCommand        theCmd        = {rateMultiplierCmd, kInit, kInit};
  1084.     OSErr            theErr        = noErr;
  1085.  
  1086.     if (StrictIsValid (theSoundInfo)) {
  1087.         theCmd.param2 = (long)theRateMul;
  1088.         theErr = SndDoImmediate(theSoundInfo->chan, &theCmd);
  1089.     }
  1090.     else {
  1091.         theErr = kNilPtrErr;
  1092.     }
  1093.  
  1094.     if (theErr != noErr) {
  1095.         DebugPrint ("\pError in ASoundSetRateMul");
  1096.     }
  1097.  
  1098.     return theErr;
  1099. }
  1100.